home *** CD-ROM | disk | FTP | other *** search
- Path: svnews.ubinet.ubs.com!ubszh!ian.johnston@ubs.com
- From: ian.johnston@ubs.com (Ian Johnston (by ubsswop))
- Newsgroups: comp.lang.c++
- Subject: Re: Simple elastic list in C++...
- Date: 10 Apr 1996 09:09:05 GMT
- Organization: UBS
- Distribution: world
- Message-ID: <4kftrh$2h3@ubszh.fh.zh.ubs.com>
- References: <4ka4d5$cia@park.interport.net>
- NNTP-Posting-Host: nol2179.fh.zh.ubs.com
-
- In article <4ka4d5$cia@park.interport.net>, scamhi@interport.net (Steven Camhi) writes:
- |> As a programmer old to C, new to C++, I have seen no 'simple' answer to the
- |> following situation. In most cases, the easiest, simplest way (for me, that
- |> is), to create a growable list in straight ANSI C, has been the following:
- |>
- |> typedef struct MyDataS
- |> {
- |> ..
- |> } MyDataT;
- |>
- |> void MyFunction()
- |> {
- |> int allocCount = 0;
- |> int rowsRead = 0;
- |> int moreElements;
- |> MyDataT *listPtr;
- |>
- |> for (moreElements = 1; moreElements; rowsRead++)
- |> {
- |> if (allocCount == rowsRead)
- |> {
- |> if (allocCount == 0)
- |> listPtr = malloc(sizeof(MyDataT) *
- |> (allocCount += 10));
- |> else
- |> listPtr = realloc(listPtr, sizeof(MyDataT) *
- |> (allocCount += 10));
- |> if (listPtr == NULL)
- |> /*
- |> * Do some error handling here.
- |> */
- |> }
- |> moreElements = ReadAnElement(&listPtr[rowsRead]);
- |> }
- |> }
- |>
- |> (Sorry if the syntax is off a little, you get the jist... :))
- |>
- |> Anyway, this model has worked for me because, you can now go ahead and further
- |> bsearch and qsort away to your hearts content, without fancy 'next pointer'
- |> fields and all that hassle.
- |>
- |> What is *the best* way of doing dynamically growable lists in C++, without a
- |> headache? Perhaps there is no *best* way, what is the way to approach the
- |> problem of manufacturing a simple list class. One way I have seen is the use
- |> of the STL vector classes; thats been my frame of reference.
-
- There is no *best* way. There are tradeoffs to be made.
-
- If your data type is a simple C-style struct, you can continue to use what you
- show above. Although using an STL vector would be more compact and reduce the
- possibilities for introducing bugs.
-
- If your data type is more complex, and needs a constructor, destructor and copy
- constructor, then growing an array like this could prove expensive in runtime.
- Then maybe you should consider other data structures.
-
- A linked list is the easiest to build, and you could sort it with a merge sort, but
- you can't bsearch it efficiently. You can create a singly linked list with one
- pointer's worth of overhead per data item.
-
- A binary tree would keep the data sorted, and is efficient to search. If you know
- the data is arriving in sorted order, you can use a simple binary tree and an
- algorithm to populate the tree using a small stack to hold certain nodes until
- they can be inserted in the right place. Or you could use one of the balancing
- trees, such as a red-black tree or AVL tree. A tree incurs a bit more overhead
- per data item; at least two pointers, maybe three, plus (theoretically, one or two
- bits, in practice at least) a char to manage the balancing.
-
-
- |> Most books I've read show a simple example of C++ by implementing a stack in
- |> C++ which inside contains a fixed size array. This has never been a 'real
- |> world' example for me.
-
- Look around for some good books on C++ and algorithms: they do exist. (Sorry,
- I don't have any references handy right now.)
-
- |> I've seen some examples on the net use realloc(), and I've heard thats a major
- |> no-no! Whats the right way?
-
- This is definitely a no-no if your data type needs a constructor, destructor and
- copy constructor. They won't be called by realloc().
-
- Ian
-